package be.kuleuven.mume.rest;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.json.JSONArray;
import org.json.JSONObject;

import be.kuleuven.mume.JSONHelper;
import be.kuleuven.mume.beans.PersoonBean;
import be.kuleuven.mume.beans.VakBean;
import be.kuleuven.mume.exceptions.UserNotLoggedInException;
import be.kuleuven.mume.query.PersoonQuery;
import be.kuleuven.mume.query.VakQuery;

@Path("/user")
public class Persoon {
	
	private static final Logger log = Logger.getLogger(Persoon.class.getName());
	
	@GET
	@Path("/")
	@Produces ("application/json")
	public String getCurrentPersoonInfo(@QueryParam("continue")@DefaultValue("api/user")String cont){
		try{
			PersoonBean persoon = PersoonQuery.getCurrentPersoon(cont);
			
			JSONObject p = new JSONObject(persoon);
			return JSONHelper.convertToJSONResult(p).toString();
		} catch (UserNotLoggedInException e) {
			return e.toJsonString();
		} catch (Exception e) {
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
	
	@GET
	@Path("/addvak/{vakid}")
	@Produces ("application/json")
	public String addVak(@PathParam("vakid") String vakid){
		try {
			PersoonBean persoon = PersoonQuery.getCurrentPersoon("/addvak/"+vakid);
			boolean succ_1 = PersoonQuery.addVak(persoon.getId(), vakid);
			boolean succ_2 = VakQuery.addStudent(vakid, persoon.getId());
			if(succ_1 && succ_2)
				return JSONHelper.SUCCES;
			else {
				if(succ_1) PersoonQuery.removeVak(persoon.getId(), vakid);
				if(succ_2) VakQuery.removeStudent(vakid, persoon.getId());
			}
		} catch (UserNotLoggedInException e) {
			return e.toJsonString();
		} catch (Exception e){
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
	
	@GET
	@Path("/removevak/{vakid}")
	@Produces ("application/json")
	public String removeVak(@PathParam("vakid") String vakid){
		try {
			PersoonBean persoon = PersoonQuery.getCurrentPersoon("/removevak/"+vakid);
			boolean succ_1 = PersoonQuery.removeVak(persoon.getId(), vakid);
			boolean succ_2 = VakQuery.removeStudent(vakid, persoon.getId());
			if(succ_1 && succ_2)
				return JSONHelper.SUCCES;
		} catch (UserNotLoggedInException e) {
			return e.toJsonString();
		} catch (Exception e){
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
	
	@GET
	@Path("/vak")
	@Produces("application/json")
	public String getVakken() {
		try {
		PersoonBean persoon = PersoonQuery.getCurrentPersoon("");
		List<String>vakken = persoon.getVakken();
		JSONArray array = new JSONArray();
		for (String vakId : vakken) {
			VakBean vak = VakQuery.getVak(vakId);
			array.put(new JSONObject(vak));
		}
		
		return JSONHelper.convertToJSONResult(array).toString();
		
		} catch (UserNotLoggedInException e) {
			return e.toJsonString();
		} catch (Exception e) {
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
	
	@POST
	@Path("/update")
	@Consumes("application/json")
	@Produces ("application/json")
	public String updatePersoon(String jsonPersoon){
		return JSONHelper.NOTIMPLEMENTED;
	}
	
	@GET
	@Path("/delete")
	@Produces ("application/json")
	public String deletePersoon(@QueryParam("continue")@DefaultValue("/index.html")String cont){
		try{
			PersoonBean persoon = PersoonQuery.getCurrentPersoon(cont);
			for (String vak : persoon.getVakken()) {
				VakQuery.removeStudent(vak, persoon.getId());
			}			
			PersoonQuery.delete(persoon.getId());
			return JSONHelper.SUCCES;
		} catch (Exception e){
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
}
